{extend name="public/base" /}

{block name="title"}角色管理{/block}
{block name="body"}
	<div class="layui-card">
		<div class="layui-card-body">
			<form class="layui-form" action="">
				<div class="layui-form-item">
					<div class="layui-form-item layui-inline">
						<label class="layui-form-label">角色筛选</label>
						<div class="layui-input-inline">
							<select name="id" lay-filter="LAY-user-adminrole-type">
								<option value="">全部角色</option>
								{volist name="roles" id="vo"}
								<option value="{$vo.id}">{$vo.title}</option>
								{/volist}
							</select>
						</div>
					</div>
					
					<div class="layui-form-item layui-inline">
						<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="role-query">
							<i class="layui-icon layui-icon-search"></i>
							查询
						</button>
						<button type="reset" class="pear-btn pear-btn-md">
							<i class="layui-icon layui-icon-refresh"></i>
							重置
						</button>
					</div>
				</div>
			</form>
		</div>
	</div>
	<div class="layui-card">
		<div class="layui-card-body">
			<table id="role-table" lay-filter="role-table"></table>
		</div>
	</div>
	
	<script type="text/html" id="role-toolbar">
		<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
			<i class="layui-icon layui-icon-add-1"></i>
			新增
		</button>
		<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
			<i class="layui-icon layui-icon-delete"></i>
			删除
		</button>
	</script>
	
	<script type="text/html" id="role-bar">
		<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" title="编辑"><i class="layui-icon layui-icon-edit"></i></button>
		<button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="power" title="权限"><i class="layui-icon layui-icon-vercode"></i></button>
		<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" title="删除"><i class="layui-icon layui-icon-delete"></i></button>
	</script>
	
	<script type="text/html" id="role-enable">
		<input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="role-enable"  {{ d.check== 1 ? 'checked' : '' }} />
	</script>

	{__block__}

	<script>
		// 数据接口
		const ROLE_LIST = "{:url('system.AuthGroup/list')}";
		const ROLE_DELETE = "{:url('system.AuthGroup/delete')}";

		layui.use(['table','form','jquery','common'],function () {
			let table = layui.table;
			let form = layui.form;
			let $ = layui.jquery;
			let common = layui.common;
	
			let MODULE_PATH = "operate/";
	
			let cols = [
				[
					{type:'checkbox'},
					{title: '角色名', field: 'rolename', align:'center', width:100},
					{title: 'Key值', field: 'roleCode', align:'center'},
					{title: '描述', field: 'descr', align:'center'},
					{title: '是否可用', field: 'check', align:'center', templet:'#role-enable'},
					{title: '操作', toolbar: '#role-bar', align:'center', width:195}
				]
			]
	
			table.render({
				elem: '#role-table',
				url: ROLE_LIST,
				page: true ,
				cols: cols ,
				skin: 'line',
				toolbar: '#role-toolbar',
				defaultToolbar: [{
					title: '刷新',
					layEvent: 'refresh',
					icon: 'layui-icon-refresh',
				}, 'filter', 'print', 'exports']
			});
	
			table.on('tool(role-table)', function(obj){
				if(obj.event === 'remove'){
					window.remove(obj);
				} else if(obj.event === 'edit'){
					window.edit(obj);
				} else if(obj.event === 'power'){
					window.power(obj);
				}
			});
	
			table.on('toolbar(role-table)', function(obj){
				if(obj.event === 'add'){
					window.add();
				} else if(obj.event === 'refresh'){
					window.refresh();
				} else if(obj.event === 'batchRemove'){
					window.batchRemove(obj);
				}
			});
	
			form.on('submit(role-query)', function(data){
				table.reload('role-table',{where:data.field})
				return false;
			});
	
			form.on('switch(role-enable)', function(obj){
				layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
				var status = obj.elem.checked ? 1 : -1;
				//执行用户审核
					$.ajax({
						type:'post',
						url:"{:url('system.AuthGroup/check')}",
						data:{"id":this.value,"status":status},
						dataType:'json',
						success:function(res){
							if(res.code === 0){
								layer.msg(res.msg,{
									icon:res.icon,
									time:2000
								}
								//,function(){location.reload();}
								);
							} else {
								layer.open({
									title:'审核失败',
									content:res.msg,
									icon:5,
									adim:6
								})
							}
						}
					});
					return false;
			});
	
			window.add = function(){
				layer.open({
					type: 2,
					title: '新增',
					shade: 0.1,
					area: ['600px', '800px'],
					content: 'add.html'
				});
			}
	
			window.power = function(obj){
				layer.open({
					type: 2,
					title: '授权',
					shade: 0.1,
					area: ['320px', '400px'],
					content: 'auth.html?id=' + obj.data.id
				});
			}
	
			window.edit = function(obj){
				layer.open({
					type: 2,
					title: '修改',
					shade: 0.1,
					area: ['600px', '800px'],
					content: 'edit.html?id=' + obj.data.id
				});
			}
	
			window.remove = function(obj){
				layer.confirm('确定要删除该角色', {icon: 3, title:'提示'}, function(index){
					layer.close(index);
					let loading = layer.load();
					$.ajax({
						url: ROLE_DELETE + "?id=" + obj.data['id'],
						dataType:'json',
						type:'delete',
						success:function(result){
							layer.close(loading);
							if(result.code === 0){
								layer.msg(result.msg,{icon:1,time:1000},function(){
									obj.del();
								});
							}else{
								layer.msg(result.msg,{icon:2,time:1000});
							}
						}
					})
				});
			}
	
			window.batchRemove = function(obj){
				let data = table.checkStatus(obj.config.id).data;
				//var checkIds = common.checkField(obj,'id'); // 选择id
				if(data.length === 0){
					layer.msg("未选中数据",{icon:3,time:1000});
					return false;
				}
				let ids = "";
				for(let i = 0;i<data.length;i++){
					ids += data[i].id+",";
				}
				ids = ids.substr(0,ids.length-1);
	
				layer.confirm('确定要删除这些用户', {icon: 3, title:'提示'}, function(index){
					layer.close(index);
					let loading = layer.load();
					$.ajax({
						url: ROLE_DELETE,
						dataType:'json',
						data:{"id":ids},
						type:'delete',
						success:function(result){
							layer.close(loading);
							if(result.code === 0){
								layer.msg(result.msg,{icon:1,time:1000},function(){
									table.reload('role-table');
								});
							}else{
								layer.msg(result.msg,{icon:2,time:1000});
							}
						}
					})
				});
			}
	
			window.refresh = function(){
				table.reload('role-table');
			}
		})
	</script>

{/block}